package com.yzh.luckydraw.interfaces.facade;

import com.alibaba.fastjson.JSON;
import com.yzh.luckydraw.application.process.deploy.IActivityDeployProcess;
import com.yzh.luckydraw.common.Result;
import com.yzh.luckydraw.domain.activity.model.aggregates.ActivityInfoLimitPageRich;
import com.yzh.luckydraw.domain.activity.model.req.ActivityInfoLimitPageReq;
import com.yzh.luckydraw.domain.activity.model.vo.ActivityVO;
import com.yzh.luckydraw.interfaces.assembler.ActivityMapping;
import com.yzh.luckydraw.rpc.activity.deploy.ILuckyDrawActivityDeploy;
import com.yzh.luckydraw.rpc.activity.deploy.dto.ActivityDTO;
import com.yzh.luckydraw.rpc.activity.deploy.req.ActivityPageReq;
import com.yzh.luckydraw.rpc.activity.deploy.res.ActivityRes;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
@DubboService
public class LuckyDrawActivityDeploy implements ILuckyDrawActivityDeploy {
    private Logger logger = LoggerFactory.getLogger(LuckyDrawActivityDeploy.class);
    @Resource
    private IActivityDeployProcess activityDeployProcess;
    @Resource
    private ActivityMapping activityMapping;

    @Override
    public ActivityRes queryActivityListByPageForErp(ActivityPageReq req) {
        try {
            logger.info("活动部署分页数据查询开始 erpID：{}", req.getErpId());

            //封装请求信息
            ActivityInfoLimitPageReq activityInfoLimitPageReq = new ActivityInfoLimitPageReq(req.getPage(), req.getRows());
            activityInfoLimitPageReq.setActivityId(req.getActivityId());
            activityInfoLimitPageReq.setActivityName(req.getActivityName());

            //查询结果
            ActivityInfoLimitPageRich activityInfoLimitPageRich = activityDeployProcess.queryActivityInfoLimitPage(activityInfoLimitPageReq);
            Long count = activityInfoLimitPageRich.getCount();
            List<ActivityVO> activityVOList = activityInfoLimitPageRich.getActivityVOList();

            //对象转换
            List<ActivityDTO> activityDTO = activityMapping.sourceToTarget(activityVOList);

            ActivityRes activityRes = new ActivityRes(Result.buildSuccessResult(), count, activityDTO);

            logger.info("活动部署分页数据查询完成 erpID：{} count：{}", req.getErpId(), count);
            return activityRes;
        } catch (Exception e) {
            logger.info("活动部署分页数据查询失败 erpID：{} count：{}", req.getErpId(), JSON.toJSON(req), e);
            return new ActivityRes(Result.buildErrorResult());
        }
    }
}
